#include "Em.h"
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);  // Pin10为RX，接HC05的TXD  - Pin11为TX，接HC05的RXD 

EM::EM(){
    sensorValue = 0;        // value read from the pot
    smokeValue = 0;
    flameValue = 0;
    LEDValue = 0;
    soundSwitch = 0;
}

void EM::initial(){
  Serial.begin(9600);
  BT.begin(9600);
  
  //input data
  pinMode(lightSensor,INPUT);
  pinMode(tempHumidityPin,INPUT);
  pinMode(smokeInPin,INPUT);
  pinMode(flameInPin,INPUT); 
  pinMode(Echo,INPUT);
  
  //output data
  pinMode(Trig,OUTPUT);
  pinMode(lightLED,OUTPUT);
  pinMode(LED,OUTPUT);
  pinMode(Alarm,OUTPUT);
  
  //define data
  digitalWrite(lightLED,LOW);
  digitalWrite(LED,LOW);
  digitalWrite(Alarm,LOW);
}

void EM::automatic_LED(){
   if(sensorValue <= 150)
     digitalWrite(lightLED,HIGH);
   else
     digitalWrite(lightLED,LOW);
}
void EM::active(){
  analogWrite(Alarm,50); 
  BT_display_data();
  Serial_display_data();
}

void EM::automatic_alarm(){
  //flame soundWave smoke
  if(soundSwitch == 1){
   if(flameValue == 1)
     if(smokeValue < 300 )
       if(soundCM > 60.00 )
          digitalWrite(Alarm,LOW); 
       else
          active(); 
     else
         active(); 
   else
      active();
  }
  else{
    if(flameValue == 1)
     if(smokeValue < 300 )
        digitalWrite(Alarm,LOW);  
     else
        active();   
   else
      active();
  }
}

void EM::humidity(){
  DHT.read11(tempHumidityPin);  //read data
}

void EM::light(){
  sensorValue = analogRead(lightSensor); 
}

void EM::smoke(){
  smokeValue = analogRead(smokeInPin); 
}

void EM::flame(){
  flameValue = digitalRead(flameInPin);
}

void EM::soundWave(){
  digitalWrite(Trig,LOW); delayMicroseconds(2);
  digitalWrite(Trig,HIGH); delayMicroseconds(10);
  digitalWrite(Trig,LOW);  
  soundTemp = float(pulseIn(Echo,HIGH)); //存储回波等待时间,
  //pulseIn函数会等待引脚变为HIGH,开始计算时间,再等待变为LOW并停止计时.
  soundCM = ((soundTemp * 340 )/10000)/2;   
}

void EM::Serial_display_data(){
   Serial.println("/*********************************/");  
   Serial.println("The environmental monitoring ! ");     
   //humidity
   Serial.print("Humidity (%): \t");
   Serial.println(DHT.humidity,2);  
   Serial.print("Temperature (oC): \t");
   Serial.println(DHT.temperature,2);
      
   //light    
   Serial.print("Brightness: \t");
   if(sensorValue >= 600){
     Serial.println("glare");
   }
   else if(sensorValue <= 600 && sensorValue >= 150){      
     Serial.println("natural light");  
   }
   else {
      Serial.println("dark");  
   }

   //smoke
   Serial.print("Smoke: \t");
   Serial.print(smokeValue);  
   if(smokeValue <= 300){
      Serial.println("\t , nomality");
   } 
   else{     
      Serial.println("\t , abnomality");
   }
          
   // flame
   if(flameValue == 0){
      Serial.println("Flame: Flame detected...!\t");
   }
   else{
      Serial.println("Flame: No flame detected...!\t");
   }
      
   //soundWave
   Serial.print("Something close :\t");
   if(soundCM <= 60.00){
       Serial.print("Yes, \t");
       Serial.print(soundCM);//串口输出距离换算成cm的结果
       Serial.println("cm");  
    }
    else{
       Serial.println("No\t");  
    }
      
    Serial.println("/**************Ending*************/\t");  
    Serial.println("");
}

void EM::BT_display_data(){
   BT.println("/*****************************/");  
   BT.println("The environmental monitoring ! ");     
   //humidity
   BT.print("Humidity (%): ");
   BT.println(DHT.humidity,2);  
   BT.print("Temperature (oC): ");
   BT.println(DHT.temperature,2);
      
   //light    
   BT.print("Brightness: ");
   if(sensorValue >= 600){
     BT.println("glare");
   }
   else if(sensorValue <= 600 && sensorValue >= 150){      
     BT.println("natural light");  
   }
   else {
      BT.println("dark");  
   }

   //smoke
   BT.print("Smoke: ");
   BT.print(smokeValue);  
   if(smokeValue <= 300){
      BT.println(" , nomality");
   } 
   else{     
      BT.println(" , abnomality");
   }
          
   // flame
   if(flameValue == 0){
      BT.println("Flame: Flame detected...!");
   }
   else{
      BT.println("Flame: No flame detected...!");
   }
      
   //soundWave
   BT.print("Something close :");
   if(soundCM <= 60.00){
       BT.print("Yes, ");
       BT.print(soundCM);//串口输出距离换算成cm的结果
       BT.println("cm");  
    }
    else{
       BT.println("No");  
    }
      
    BT.println("/************Ending***********/");  
    BT.println("");
}

void EM::conditional_judgment(char condition){
  switch(condition){
      //receive data 
      case ('0') :
        BT_display_data();
        Serial_display_data();
        break;
        
      //LED 
      case ('1') ://LED OPEN 
        analogWrite(LED,200);       
        LEDValue = 200;      
        break; 
      case ('2') ://LED close
        digitalWrite(LED,LOW);  
        LEDValue = 0;      
        break; 
        
      case ('3') : //LED ADD
        if(LEDValue == 200)
          analogWrite(LED,100);
        else
          digitalWrite(LED,LOW); 
        break;
      case ('4') : //LED MINUS
        if(LEDValue == 200)
          analogWrite(LED,255);
        else
          digitalWrite(LED,LOW);  
        break;
        
       //prevent   
      case ('5') ://sound close
        soundSwitch = 0;
        break;
      case ('6') ://sound open
        soundSwitch = 1;
        break;  
    }
}

void EM::wireless_control(){
  if(BT.available()){
     valueBT = BT.read();
     conditional_judgment(valueBT);
  }
}

void EM::wire_control(){
  if(Serial.available()){
     valueSerial = Serial.read();
     conditional_judgment(valueSerial);
  }
}
